<?php
/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2016-12-14 22:09:44
 * @Description:
 */

class Api_AdminUser extends PhalApi_Api {

	public function getRules() {
		return array(
			'login' => array(
				'username' => array('name' => 'username', 'require' => true, 'type' => 'string', 'min' => 6, 'max' => 18, 'desc' => '用户名'),
				'password' => array('name' => 'password', 'require' => true, 'type' => 'string', 'min' => 6, 'max' => 18, 'desc' => '密码'),
			),
		);
	}

	/**
	 * 用户登陆接口服务类 - login
	 * @desc 用户通用接口
	 * @return int code 操作码, 0 登录成功 ,1 用户名不存在, 2 密码错误, 3 系统已屏蔽此账号, 4 密码错误次数超过了最大的重试次数
	 * @return object info 用户信息对象
	 * @return string msg 提示信息
	 */
	public function login() {
		//var_dump($_SESSION['username']);
		//var_dump(md5($this->password));
		//var_dump($_SERVER['HTTP_REFERER']);
		//echo '<pre>';
		//var_dump(DI()->config);
		//print_r(DI()->config->get('sys.cookie')['domain']);
		//echo '</pre>';
		//die();
		//if ($_SERVER['HTTP_REFERER']) {}

		$array = array('int' => 1, 'info' => '', 'time' => $_SERVER['REQUEST_TIME']);
		$row = DI()->notorm->admin->select('*')->where('username', $this->username)->fetch();
		if ($row) {
			if ($row['password'] == md5($this->password)) {
				$_SESSION['username'] = $this->username;
				DI()->cookie->set(
					'token',
					md5($this->username + $this->password + $_SERVER['HTTP_USER_AGENT']),
					$_SERVER['REQUEST_TIME'] + 600
				);
				$array['int'] = 0;
				$array['info'] = $row;

			} else {
				$array['int'] = 2;
				$array['info'] = '';
			}
		} else {
			$array['int'] = 1;
			$array['info'] = '';
		}
		//print_r($row);
		//var_dump($row);
		return $array;
	}

	public function listUser() {
		$array = array('int' => 1, 'info' => '', 'time' => $_SERVER['REQUEST_TIME']);
		$row = DI()->notorm->admin->select('*')->fetchAll();
		if ($row) {
			$array['int'] = 0;
			$array['info'] = $row;
		} else {
			$array['int'] = 1;
			$array['info'] = '';
		}
		//print_r($row);
		//var_dump($row);
		//return json_encode($array);
		return $array;
	}
}
